from django.db import models
# from django.contrib.auth.models import User
# from django.conf import settings
from Login.models import User

# Create your models here.

class SportTrack(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    name = models.CharField(max_length=100, default='运动轨迹')
    total_distance = models.FloatField(default=0)

    def __str__(self):
        return f"{self.user.username} - {self.name} - {self.start_time}"

class SportTrackPoint(models.Model):
    track = models.ForeignKey(SportTrack, on_delete=models.CASCADE, related_name='points')
    lat = models.FloatField()
    lng = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.track.id} - {self.lat},{self.lng} - {self.timestamp}"

class SportRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    date = models.DateField(verbose_name='运动日期')
    type = models.CharField(max_length=20, verbose_name='运动类型')
    duration = models.IntegerField(verbose_name='时长(分钟)')
    distance = models.FloatField(verbose_name='距离(公里)')
    calories = models.FloatField(verbose_name='消耗(千卡)')
    note = models.CharField(max_length=200, blank=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '运动记录'
        verbose_name_plural = '运动记录'
        ordering = ['-date', '-created_at']

class WeightRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    date = models.DateField(verbose_name='记录日期')
    weight = models.FloatField(verbose_name='体重(kg)')
    note = models.CharField(max_length=200, blank=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '体重记录'
        verbose_name_plural = '体重记录'
        ordering = ['-date', '-created_at']

class DietRecord(models.Model):
    MEAL_CHOICES = (
        ('breakfast', '早餐'),
        ('lunch', '午餐'),
        ('dinner', '晚餐'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    date = models.DateField(verbose_name='记录日期')
    meal_type = models.CharField(max_length=10, choices=MEAL_CHOICES, verbose_name='餐次')
    food = models.CharField(max_length=100, verbose_name='食物名称')
    weight = models.FloatField(verbose_name='食物重量(克)')
    note = models.CharField(max_length=200, blank=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '饮食记录'
        verbose_name_plural = '饮食记录'
        ordering = ['-date', '-created_at']

class FoodNutrition(models.Model):
    name = models.CharField(max_length=100, unique=True, verbose_name='食物名称')
    energy = models.FloatField(verbose_name='能量(千卡/100g)')
    # 可扩展蛋白质、脂肪、碳水等字段
    def __str__(self):
        return f"{self.name} - {self.energy} kcal/100g"

class DeviceData(models.Model):
    """
    设备数据模型：用于存储用户运动过程中的设备数据（如心率、步频等）。
    可关联用户、运动轨迹。
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 关联用户
    track = models.ForeignKey('SportTrack', on_delete=models.CASCADE, null=True, blank=True)  # 关联运动轨迹
    heart_rate = models.IntegerField(null=True, blank=True)  # 心率（bpm）
    step = models.IntegerField(null=True, blank=True)        # 步频/步数
    timestamp = models.DateTimeField(auto_now_add=True)      # 数据采集时间

    def __str__(self):
        return f"{self.user_id} - {self.heart_rate}bpm - {self.timestamp}"
